/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sfs2x.extension.fourinarow.main;

import com.athena.objects.Game;
import com.athena.objects.Player;
import com.smartfoxserver.v2.core.ISFSEvent;
import com.smartfoxserver.v2.core.SFSEventParam;
import com.smartfoxserver.v2.entities.Room;
import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSArray;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSArray;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.smartfoxserver.v2.exceptions.SFSException;
import com.smartfoxserver.v2.extensions.BaseServerEventHandler;
import java.util.Enumeration;
import java.util.List;
import sfs2x.extension.fourinarow.utils.Commands;

/**
 *
 * @author dodinhchien
 */
public class UserjoinRoomHandle extends BaseServerEventHandler {

    @Override
    public void handleServerEvent(ISFSEvent event) throws SFSException {
        Room room = (Room) event.getParameter(SFSEventParam.ROOM);
        trace(room.getUserList());
        if (room.isGame()) {
            User user = (User) event.getParameter(SFSEventParam.USER);
            Game curGame = ((FourinarowExtension) getParentExtension()).getGamesList().get(room.getId());
            if (curGame == null) {
                int xu = room.getVariable("xu").getIntValue();
                curGame = new Game(room, xu);
                ((FourinarowExtension) getParentExtension()).getGamesList().put(room.getId(), curGame);
            }

            if (curGame != null) {
                Player player = ((FourinarowExtension) getParentExtension()).getUsersList().get(user.getId());
                player.setRoomId(room.getId());
                curGame.getPlayers().put(user.getId(), player);
                
                List<User> users = room.getUserList();
                for (User user1 : users) {
                    ISFSObject obj = new SFSObject();
                    if (user == user1) {
                        ISFSArray arr = new SFSArray();
                        for (Enumeration<Player> e = curGame.getPlayers().elements(); e.hasMoreElements();) {
                            Player p = e.nextElement();
                            ISFSObject subo = new SFSObject();
                            subo.putUtfString("N", p.getUsername());
                            subo.putInt("M", p.getMark());
                            arr.addSFSObject(subo);
                        }
                        obj.putSFSArray("arr", arr);
                        obj.putInt("MU", curGame.getMark());
                        send(Commands.JOIN_ROOM, obj, user);
                    } else {
                        obj.putUtfString("N", user.getName());
                        obj.putInt("M", player.getMark());
                        send(Commands.OTHER_JOIN, obj, user1);
                    }
                }

            }

        }
    }

}
